package cn.szzsi.business.service;

import cn.szzsi.core.kit.CommonUtil;
import cn.szzsi.model.AgentBalance;
import com.jfinal.kit.StrKit;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 代理商余额业务类
 */
public class AgentBalanceService {
    public static final AgentBalanceService service=new AgentBalanceService();

    /**
     * 余额明细,若表里面不存在该会员,才会创建,否则直接更新,不执行此方法
     * @param agentId 代理商id
     * @param balance 代理商资金余额
     * @param cashing 代理商提现金额
     */
    public void saveInfo(String agentId, BigDecimal balance, BigDecimal cashing){
        AgentBalance agentBalance=new AgentBalance();
        agentBalance.setId(StrKit.getRandomUUID());
        agentBalance.setAgentId(agentId);
        agentBalance.setBalance(balance);
        agentBalance.setCashingMoney(cashing);
        agentBalance.setCreateDate(new Date());
        agentBalance.save();
    }

    /**
     *  更新余额
     * @param agentId 代理商id
     * @param balance 余额
     */
    public void updateBalance(String agentId, BigDecimal balance){
        //1.查看结果表是否存在该会员信息
        AgentBalance agentBalance= AgentBalance.dao.getByAgentId(agentId);
        if(agentBalance!=null){//若存在,则直接更新
            agentBalance.setBalance(agentBalance.getBalance().add(balance));
            agentBalance.setUpdateDate(new Date());
            agentBalance.update();
        }else{//2.若不存在,则新增
            saveInfo(agentId,balance,new BigDecimal(0));
        }
    }
}
